//
//  LPHomeMenuIndexDetailListViewController.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPHomeMenuIndexDetailCourseListViewController.h"
#import "ZJVidoViewController.h"
#import "LPZWYChooseClassClassModel.h"
#import "LPZWYCCTableHeaderView.h"
#import "LPZWYCommonClassTableViewCell.h"
#import "LPZWYChooseClassMoreClassViewController.h"
#define classCell @"LPZWYCommonClassTableViewCell"
@interface LPHomeMenuIndexDetailCourseListViewController ()<UITableViewDelegate,UITableViewDataSource,LPZWYCCTableHeaderViewDelegate>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , copy) NSMutableArray * dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/**    */
@property (strong, nonatomic) NSMutableArray  *orangalArray;


@end

@implementation LPHomeMenuIndexDetailCourseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"选课";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    [self.orangalArray removeAllObjects];
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getcoursewarelistforwcateWithsearchType:@"1"  pageindex:[NSString stringWithFormat:@"%ld",self.page] pagesize:@"3" successBlock:^(id object) {
        NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
        if ([result integerValue] == 1) {
            NSArray *array = [[object objectForKey:@"data"] objectForKey:@"list"];
            
            [weakSelf.orangalArray addObject:@{@"title": @"最新课程", @"data": [NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]}];
            
            [weakSelf getHot];
            
        }else{
            [self.tableView.mj_header endRefreshing];
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        
        
    } failBlock:^(id object) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        
    }];
    
}

- (void)getHot{
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getcoursewarelistforwcateWithsearchType:@"2"  pageindex:[NSString stringWithFormat:@"%ld",self.page] pagesize:@"3" successBlock:^(id object) {
        NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
        if ([result integerValue] == 1) {
            NSArray *array = [[object objectForKey:@"data"] objectForKey:@"list"];
            
            [weakSelf.orangalArray addObject:@{@"title": @"最热课程", @"data": [NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]}];
            
            [weakSelf getTuijian];
            
        }else{
            [self.tableView.mj_header endRefreshing];
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        
        
    } failBlock:^(id object) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        
    }];
}

- (void)getTuijian{
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getcoursewarelistforwcateWithsearchType:@"4"  pageindex:[NSString stringWithFormat:@"%ld",self.page] pagesize:@"3" successBlock:^(id object) {
        NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
        if ([result integerValue] == 1) {
            NSArray *array = [[object objectForKey:@"data"] objectForKey:@"list"];
            
            [weakSelf.orangalArray addObject:@{@"title": @"推荐课程", @"data": [NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]}];
            weakSelf.dataArray = weakSelf.orangalArray;
            [weakSelf.tableView reloadData];
            
        }else{
            [self.tableView.mj_header endRefreshing];
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        [self.tableView.mj_header endRefreshing];
        
    } failBlock:^(id object) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        
    }];
}





#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.navigView , 0)
    .bottomSpaceToView(self.view , 0);
    
    
}


#pragma mark - 私有方法


#pragma mark - 监听事件



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [self.dataArray[section] objectForKey:@"data"];
    return array.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array = [self.dataArray[indexPath.section] objectForKey:@"data"];
    LPZWYCommonClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
    cell.isMintue = YES;
    cell.model = array[indexPath.row];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * [UIScreen zj_screenHScale];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LPZWYCCTableHeaderView *header = [[LPZWYCCTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 40 * [UIScreen zj_screenHScale])];
    header.section = section;
    header.delegate = self;
    NSString *title = [self.dataArray[section] objectForKey:@"title"];
    header.headline.text = title;
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    TenGrayShadowView *ten = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 15 * [UIScreen zj_screenHScale])];
    return ten;
}



- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    return 15 * [UIScreen zj_screenHScale];
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    
    if (scrollView ==_tableView)
        
    {
        CGFloat sectionHeaderHeight = 40 * [UIScreen zj_screenHScale];
        CGFloat sectionFooterHeight = 15 * [UIScreen zj_screenHScale];
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
        }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
        }else if (offsetY >= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight), 0);
        }
    }
    
}

// LPZWYCCTableHeaderViewDelegate
- (void)LPZWYCCTableHeaderView_clickMore:(NSInteger)section{
    
    
    LPZWYChooseClassMoreClassViewController *vc =
    [LPZWYChooseClassMoreClassViewController new];
    vc.classType = [self.dataArray[section] objectForKey:@"title"];
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    
    
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (YBUSERINFOMODEL.isCloudyAutonomouslyChooseStudy) {
        ZJVidoViewController *vc = [ZJVidoViewController new];
        NSArray *array = [self.dataArray[indexPath.section] objectForKey:@"data"];
        vc.model = array[indexPath.row];
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }else{
        [YJProgressHUD showSuccess:@"您没有自主选学权限!" inview:[UIApplication sharedApplication].keyWindow];
    }
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[LPZWYCommonClassTableViewCell class] forCellReuseIdentifier:classCell];
         [_tableView adaptiveIOS11];
        
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


- (NSMutableArray *)orangalArray{
    if (_orangalArray == nil) {
        _orangalArray = [NSMutableArray array];
    }
    return _orangalArray;
}


@end
